//
//  SMLoginViewController.m
//  SaveMoney
//
//  Created by user on 2018/11/30.
//  Copyright © 2018年 Sunflower. All rights reserved.
//

#import "SMLoginViewController.h" 
#import "AFNetworking.h"
@interface SMLoginViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *touxiangImage;
@property (weak, nonatomic) IBOutlet SBBaseTextField *accountTF;
@property (weak, nonatomic) IBOutlet SBBaseTextField *passwTF;
@property (weak, nonatomic) IBOutlet SBBaseButton *savePasswordButton;

@end

@implementation SMLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    KViewBorderRadius(_touxiangImage, SBScaleNumber(150)/2, 0.5, [UIColor blackColor]);
     
    _accountTF.text = [SMUserInfoModel sharedStorage].oldPhone;
    _passwTF.text = [SMUserInfoModel sharedStorage].oldPassword;
    if ([_accountTF.text isValid]) {
        _savePasswordButton.selected = YES;
    }
     
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (IBAction)loginAction:(id)sender {
    
    if (![_accountTF.text isValidMobileNumberOrPhoneNumber]) {
        [SVProgressHUD showErrorWithStatus:@"账号输入错误"];
        return;
    }
    if (![_passwTF.text isValid]) {
        [SVProgressHUD showErrorWithStatus:@"密码为空"];
        return;
    }
    
    [SVProgressHUD show];
    __weak typeof(self) weakSelf = self;
    [[NetworkAction sharedAction] LoginUserWithAccount:_accountTF.text password:_passwTF.text success:^(id model, NSString *message) {
        
        [weakSelf savePassword:weakSelf.savePasswordButton];
        
        NSDictionary *dic = model;
        [SMUserInfoModel sharedStorage].addtime  = dic[@"addtime"];
        [SMUserInfoModel sharedStorage].avatar   = dic[@"avatar"];
        [SMUserInfoModel sharedStorage].email    = dic[@"email"];
        [SMUserInfoModel sharedStorage].nickName = dic[@"nickName"];
        [SMUserInfoModel sharedStorage].phone    = dic[@"phone"];
        [SMUserInfoModel sharedStorage].password = dic[@"password"];
        [SMUserInfoModel sharedStorage].userKey  = dic[@"phone"];
        [SMUserInfoModel sharedStorage].isLogin  = @"yes";
        [[SMUserInfoModel sharedStorage] save];
        
        [[NetworkAction sharedAction] downLoadDataInfoWithPhone:SMUserInfoModel.sharedStorage.phone success:^(id model, NSString *message) {
            
            NSArray *dic = [self dictionaryWithJsonString:model];
            [[SMDataManage sharedStorage] saveDataWithArray:dic];
            [SVProgressHUD showSuccessWithStatus:message];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
                
                UIViewController *homePageVC = [[UIStoryboard storyboardWithName:@"SMHome" bundle:nil] instantiateInitialViewController];
                [self presentViewController:homePageVC animated:YES completion:nil];
            });
            
            
        } failuer:^(NSError *error) {
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        }];
        
    } failuer:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:[error localizedDescription]];
    }];
     
}
- (IBAction)savePassAction:(UIButton *)sender {
    
    sender.selected = sender.selected? NO: YES;
    [self savePassword:sender];
    
}

- (void)savePassword:(UIButton *)sender {
    if (sender.selected) {
        [SMUserInfoModel sharedStorage].oldPhone = _accountTF.text;
        [SMUserInfoModel sharedStorage].oldPassword = _passwTF.text;
    } else {
        [SMUserInfoModel sharedStorage].oldPhone = @"";
        [SMUserInfoModel sharedStorage].oldPassword = @"";
    }
}

- (id)dictionaryWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
